package com.example.psysystemhomework.util;

import android.content.Context;
import android.graphics.Color;

import com.example.psysystemhomework.bean.EmotionBean;
import com.example.psysystemhomework.bean.EmotionTo;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.renderer.YAxisRenderer;

import java.util.ArrayList;
import java.util.List;

public class ChartUtil {
    private PieChart piechart;
    private HorizontalBarChart horizontalBarChart;

    public ChartUtil(PieChart piechart) {
        this.piechart = piechart;
        piechart.setDescription("");
//        barChart.setDrawGridBackground(false);
//        xAxis.setDrawGridLines(false);
        piechart.setDrawHoleEnabled(false);
//        xAxis.setDrawAxisLine(false);
        piechart.getLegend().setPosition(Legend.LegendPosition.ABOVE_CHART_RIGHT);
    }


    public ChartUtil(HorizontalBarChart horizontalBarChart) {
        this.horizontalBarChart = horizontalBarChart;
        XAxis xAxis = horizontalBarChart.getXAxis();
        xAxis.setTextSize(20);
        YAxis axisLeft = horizontalBarChart.getAxisLeft();
        axisLeft.setStartAtZero(true);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        horizontalBarChart.setDescription("用户心理分析图");
        horizontalBarChart.setDescriptionTextSize(10);
//        barChart.setDrawGridBackground(false);

        horizontalBarChart.getLegend().setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
    }

    public void showPieChart(ArrayList<EmotionBean> gamerBeans, int textSize) {
        piechart.animateX(1000);
        piechart.getLegend().setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
        //index---0-5
        ArrayList<Entry> entries = new ArrayList<>();
        int p = 0;
        String[] s = new String[gamerBeans.size()];
        for (int i = 0; i < gamerBeans.size(); i++) {
            entries.add(new Entry(gamerBeans.get(i).getNum(), p));
            s[p] = gamerBeans.get(i).getName();
            p++;
        }

        int[] color = new int[]{Color.RED, Color.YELLOW, Color.argb(100, 246, 97, 26), Color.GREEN};
        PieDataSet pieDataSet = new PieDataSet(entries, "");
        pieDataSet.setColors(color);
        PieData pieData = new PieData(s, pieDataSet);
        pieData.setValueTextSize(textSize);
        pieData.setDrawValues(false);
        piechart.setData(pieData);
        piechart.invalidate();
    }


    public void showHoriBarChart(ArrayList<EmotionTo> y) {
        horizontalBarChart.animateXY(3000, 3000);
        String[] x = new String[y.size()];
        int[] colors = new int[y.size()];
        List<BarEntry> list = new ArrayList<>();
        for (int i = 0; i < y.size(); i++) {
            list.add(new BarEntry(y.get(i).getCount() , i));
            x[i] = y.get(i).getType();
            if (y.get(i).getCount() >= 3) {
                colors[i] = Color.RED;
            } else {
                colors[i] = Color.GREEN;

            }
        }


        BarDataSet barDataSet = new BarDataSet(list, "分析结果");
        barDataSet.setValueTextSize(20);
        barDataSet.setBarSpacePercent(50f);
        barDataSet.setColors(colors);
        BarData barData = new BarData(x, barDataSet);
//        barData.addDataSet(barDataSet);
//        xAxis.setValueFormatter(new IAxisValueFormatter() {
//            @Override
//            public String getFormattedValue(float value, AxisBase axis) {
//
//                return x[(int) value]
//            }
//        });

        horizontalBarChart.setData(barData);
        horizontalBarChart.invalidate();


    }


}
